<template>
    <div class="login-form">
        <h2>欢迎登录</h2>

        <el-form ref="loginFormRef" :model="loginData" :rules="loginRules">
            <!-- 用户名 -->
            <el-form-item prop="username">
                <div class="input-wrapper">
                    <el-icon class="mx-2">
                        <User />
                    </el-icon>
                    <el-input
                        ref="username"
                        v-model="loginData.username"
                        :placeholder="$t('login.username')"
                        name="username"
                        size="large"
                        class="h-[48px]"
                    ></el-input>
                </div>
            </el-form-item>

            <!-- 密码 -->
            <el-tooltip
                :visible="isCapslock"
                :content="$t('login.capsLock')"
                placement="right"
            >
                <el-form-item prop="password">
                    <div class="input-wrapper">
                        <el-icon class="mx-2">
                            <Lock />
                        </el-icon>
                        <el-input
                            v-model="loginData.password"
                            :placeholder="$t('login.password')"
                            type="password"
                            name="password"
                            size="large"
                            class="h-[48px] pr-2"
                            show-password
                            @keyup="checkCapslock"
                            @keyup.enter="handleLoginSubmit"
                        />
                    </div>
                </el-form-item>
            </el-tooltip>

            <div class="flex-x-between w-full py-1">
                <el-checkbox>
                    {{ $t("login.rememberMe") }}
                </el-checkbox>
            </div>

            <!-- 登录按钮 -->
            <el-button
                :loading="loading"
                type="primary"
                size="large"
                class="w-full"
                @click.prevent="handleLoginSubmit"
            >
                {{ $t("login.login") }}
            </el-button>
        </el-form>

        <SendMsg
            :visible="visible"
            :uid="uid"
            :tel="loginData.username"
            @update:visible="visible = $event"
        ></SendMsg>
    </div>
</template>

<script setup lang="ts">
import AuthAPI, { type LoginData } from "@/api/auth";
import type { FormInstance } from "element-plus";

const { t } = useI18n();
const loginFormRef = ref<FormInstance>();

const loading = ref(false); // 按钮 loading 状态
const isCapslock = ref(false); // 是否大写锁定
const visible = ref(false); // 弹窗是否显示
const uid: any = ref(null); // 登录返回参数

const loginData = ref<LoginData>({
    username: "16638277996",
    password: "123456a",
});

const loginRules = computed(() => {
    return {
        username: [
            {
                required: true,
                trigger: "blur",
                message: t("login.message.username.required"),
            },
        ],
        password: [
            {
                required: true,
                trigger: "blur",
                message: t("login.message.password.required"),
            },
            {
                min: 6,
                message: t("login.message.password.min"),
                trigger: "blur",
            },
        ],
    };
});

// 登录按钮点击
async function handleLoginSubmit() {
    loginFormRef.value?.validate((valid: boolean) => {
        if (valid) {
            loading.value = true;
            AuthAPI.login(loginData.value)
                .then(async (data: any) => {
                    uid.value = data.uid;
                    visible.value = true;
                })
                .catch((err: any) => {
                    console.log(err);
                    uid.value = null;
                })
                .finally(() => {
                    loading.value = false;
                });
        }
    });
}

// 检查输入大小写
function checkCapslock(event: KeyboardEvent) {
    // 防止浏览器密码自动填充时报错
    if (event instanceof KeyboardEvent) {
        isCapslock.value = event.getModifierState("CapsLock");
    }
}
</script>

<style lang="scss" scoped>
.login-form {
    cursor: pointer;
    position: relative;
    width: 100%;
    height: 100%;
    padding: 50px 25px 0 25px;
    h2 {
        font-weight: 400;
        margin-bottom: 16px;
    }
    .input-wrapper {
        display: flex;
        align-items: center;
        width: 100%;
    }

    :deep(.el-form-item) {
        background: var(--el-input-bg-color);
        border: 1px solid var(--el-border-color);
        border-radius: 5px;
    }
    :deep(.el-input) {
        .el-input__wrapper {
            padding: 0;
            background-color: transparent;
            box-shadow: none;

            &.is-focus,
            &:hover {
                box-shadow: none !important;
            }

            input:-webkit-autofill {
                /* 通过延时渲染背景色变相去除背景颜色 */
                transition: background-color 1000s ease-in-out 0s;
            }
        }
    }
}
</style>
